<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="ordCode">
        <el-input
            v-model="queryParams.ordCode"
            placeholder="请输入订单编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="evalType">
        <el-select v-model="queryParams.evalType" placeholder="请选择类型" clearable>
          <el-option
              v-for="dict in t_eval_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />

        </el-select>
      </el-form-item>
      <el-form-item label="评价对象" prop="evalObj"
                    v-if="useUserStore().userType === '0' || useUserStore().userType === '1'">
        <el-input
            v-model="queryParams.evalObj"
            placeholder="请输入评价对象"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="shopName"
                    v-if="useUserStore().userType === '0' || useUserStore().userType === '1' || useUserStore().userType === '2'">
        <el-input
            v-model="queryParams.shopName"
            placeholder="请输入店铺名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:teval:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:teval:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tevalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单编号" align="center" prop="ordCode"/>
      <el-table-column label="用户名称" align="center" prop="userName"/>
      <el-table-column label="评价对象" align="center" prop="evalObj"/>
      <el-table-column label="店铺名称" align="center" prop="shopName"
                       v-if="useUserStore().userType === '0' || useUserStore().userType === '1' || useUserStore().userType === '2'"/>
      <el-table-column label="类型" align="center" prop="evalType">
        <template #default="scope">
          <dict-tag :options="t_eval_type" :value="scope.row.evalType"/>
        </template>
      </el-table-column>
      <el-table-column label="评价时间" align="center" prop="createTime"/>
      <el-table-column label="评价评论" align="center" prop="comment"/>
      <el-table-column label="评分" align="center" prop="score"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)"
                     v-hasPermi="['system:teval:query']">详情
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     :class="{'but-disabled': userStore.isRead === 1}"
                     v-hasPermi="['system:teval:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <el-dialog title="评价详情" v-model="viewOpen" width="700px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="评价人：">{{ form.userName }}</el-form-item>
            <el-form-item
                label="订单编号："
            >{{ form.ordCode }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评价对象：">{{ form.evalObj }}</el-form-item>
            <el-form-item label="评分：">{{ form.score }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容：">{{ form.comment }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="附件：">
              <image-preview :src-list="form.fileList" :width="150" :height="150"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="viewOpen = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Teval">
import {listTeval, getTeval, delTeval, addTeval, updateTeval} from "@/api/system/teval";
import {ref} from "vue";
import useUserStore from "@/store/modules/user";

const userStore = useUserStore()
const {proxy} = getCurrentInstance();
const {t_eval_type} = proxy.useDict('t_eval_type');

const tevalList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const viewOpen = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: undefined,
    ordId: undefined,
    prodId: undefined,
    score: undefined,
    comment: undefined,
    ext1: undefined,
    ext2: undefined,
    ext3: undefined,
    state: undefined,
    modifyTime: undefined
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

/** 查询评价列表列表 */
function getList() {
  loading.value = true;
  listTeval(queryParams.value).then(response => {
    tevalList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.evalId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleView(row) {
  const _evalId = row.evalId || ids.value
  getTeval(_evalId).then(response => {
    form.value = response.data;
    viewOpen.value = true;
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _evalIds = row.evalId || ids.value;
  proxy.$modal.confirm('是否确认删除评价列表编号为"' + _evalIds + '"的数据项？').then(function () {
    return delTeval(_evalIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/teval/export', {
    ...queryParams.value
  }, `teval_${new Date().getTime()}.xlsx`)
}
onActivated(() => {
  getList();
})
getList();
</script>
